package com.ruoyi.aitymyy.service;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aitcommon.anotation.AitFlow;
import com.ruoyi.aitcommon.api.DBOperation;
import com.ruoyi.aitcommon.service.SQLService;
import com.ruoyi.aitcommon.utils.JSONResult;
import com.ruoyi.aitymyy.api.YmyyAPI;
import com.ruoyi.aitymyy.config.OuterDBConfig;
import com.ruoyi.aitymyy.utils.YmyyUtils;

@Service
public class YmyySyncSV {
	@Autowired
	DBOperation dboper;
	@Autowired
	YmyyAPI ymyyApi;
	@Autowired
	SQLService sqlsv;
	@Autowired
	YmyyUtils servutils;
	
	/**
	 * 供应商同步
	 */
	@AitFlow("供应商数据同步Flow")
	public JSONObject gysSync() throws Exception{
		String sql="select import_id from interface\r\n"
				+ "where import_id like 'distributor_20%' and record_count>0 and get_status is null\r\n"
				+ "order by import_id";
		List<Map<String, Object>> ilist = sqlsv.execQuery(sql, OuterDBConfig.zjk);
		if(ilist==null||ilist.size()==0) return JSONResult.success("没有数据");
		for(Map<String,Object> item:ilist) {
			servutils.gysImport(item.get("import_id").toString());
		}
		return JSONResult.success("导入成功");
	}

	/**
	 * 供应商地址同步
	 */
	@AitFlow("供应商地址数据同步Flow")
	public JSONObject gysaddrSync() throws Exception{
		String sql="select import_id from interface\r\n"
				+ "where import_id like 'distributorshipment%' and record_count>0 and get_status is null\r\n"
				+ "order by import_id";
		List<Map<String, Object>> ilist = sqlsv.execQuery(sql, OuterDBConfig.zjk);
		if(ilist==null||ilist.size()==0) return JSONResult.success("没有数据");
		for(Map<String,Object> item:ilist) {
			servutils.gysaddrImport(item.get("import_id").toString());
		}
		return JSONResult.success("导入成功");
	}

	

	/**
	 * 联系人同步
	 */
	@AitFlow("联系人数据同步Flow")
	public JSONObject lxrSync() throws Exception {
		String sql="select import_id from interface\r\n"
				+ "where import_id like 'contacts%' and record_count>0 and get_status is null\r\n"
				+ "order by import_id";
		List<Map<String, Object>> ilist = sqlsv.execQuery(sql, OuterDBConfig.zjk);
		if(ilist==null||ilist.size()==0) return JSONResult.success("没有数据");
		for(Map<String,Object> item:ilist) {
			servutils.lxrImport(item.get("import_id").toString());
		}
		return JSONResult.success("导入成功");
	}

	/**
	 * 部门同步服务
	 */
	@AitFlow("部门数据同步Flow")
	public JSONObject deptSync(Double days) throws Exception {
//		差异定义
		String table = "interface_department";
		String sql = "select * from v_ait_dept where ts>sysdate-%s";

		return servutils.dataSync(days, table, sql);
	}

	/**
	 * 人员同步服务
	 */
	@AitFlow("人员数据同步Flow")
	public JSONObject psnSync(Double days) throws Exception {
//		差异定义
		String table = "interface_user";
		String sql = "select * from v_ait_psn where ts>sysdate-%s";

		return servutils.dataSync(days, table, sql);
	}

	/**
	 * 订单同步服务
	 */
	@AitFlow("订单数据同步Flow")
	public JSONObject saleSync(Double days) throws Exception {
//		差异定义
		String table = "interface_order";
		String sql = "select * from v_ait_sale where ts>sysdate-%s";

		return servutils.dataSync(days, table, sql);
	}

	/**
	 * 订单发货同步服务
	 */
	@AitFlow("订单发货数据同步Flow")
	public JSONObject shipmentSync(Double days) throws Exception {
//		差异定义
		String table = "interface_order_shipment";
		String sql = "select * from v_ait_general where ts>sysdate-%s";

		return servutils.dataSync(days, table, sql);
	}

	/**
	 * 订单开票同步服务
	 */
	@AitFlow("订单开票数据同步Flow")
	public JSONObject invoiceSync(Double days) throws Exception {
//		差异定义
		String table = "interface_order_invoice";
		String sql = "select * from v_ait_saleinvoice where ts>sysdate-%s";

		return servutils.dataSync(days, table, sql);
	}

	/**
	 * 订单收款同步服务
	 */
	@AitFlow("订单回款数据同步Flow")
	public JSONObject paymentSync(Double days) throws Exception {
//		差异定义
		String table = "interface_order_payment";
		String sql = "select * from v_ait_arap where ts>sysdate-%s";

		return servutils.dataSync(days, table, sql);
	}

	
}

